/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package company.folder.beans;

import company.abstractClasses.CompanyAbstractEntityList;
import facades.CompanyFolderFacade;
import entities.Company;
import entities.CompanyFolder;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import company.folder.qualifiers.SeeAllProfilesEvent;
import entities.CompanyAdquiredProfile;
import entities.CompanyAdquiredProfileFolder;
import entities.Search;
import facades.CompanyFolderTypeFacade;
import java.util.Collections;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.event.TransactionPhase;
import javax.transaction.Transactional;
import qualifiers.CreatedEntityEvent;
import qualifiers.PreCreateEntityEvent;

/**
 *
 * @author damfercer
 */
@Named
@SessionScoped
public class NewFolderListBean extends CompanyAbstractEntityList<CompanyFolder, CompanyFolderFacade> implements Serializable {
    private CompanyFolder newFolder;
    @Inject
    private CompanyFolderTypeFacade companyFolderTypeFacade;
    @Inject
    private CompanyFolderFacade companyFolderFacade;
    @Inject
    @SeeAllProfilesEvent
    private Event<Integer> viewAllProfilesEvent;
    @Inject
    @PreCreateEntityEvent
    private Event<CompanyFolder> precreateCompanyFolder;
    @Inject
    @CreatedEntityEvent
    private Event<CompanyFolder> createdCompanyFolder;

    
    
    @PostConstruct
    public void initNewFolderListBean(){
        newFolder=new CompanyFolder();        
    }  
    
    @Override
    public void initEntityList(){
        if(getCompany().getCompanyFolderList()==null){
            getCompany().setCompanyFolderList(new ArrayList<CompanyFolder>());
        }
        setEntityList(getCompany().getCompanyFolderList());
    }
    
    public void createNewFolder(){
        newFolder.setCreationDate(new Date());
        newFolder.setIdCompany(getCompany());
        newFolder.setCompanyFolderType(companyFolderTypeFacade.getNormalFolderType());
        getEntityFacade().create(newFolder);
        if(getCompany().getCompanyFolderList()==null){
            getCompany().setCompanyFolderList(new ArrayList<CompanyFolder>());
        }
        getCompany().getCompanyFolderList().add(newFolder);
        newFolder=new CompanyFolder();
    }
    
    public void reinitNewFolder(){
        newFolder=new CompanyFolder();
    }
    
    public void editFolder(){
        getEntityFacade().edit(getChosenEntity());
        if(getCompany().getCompanyFolderList()==null){
            getCompany().setCompanyFolderList(new ArrayList<CompanyFolder>());
        }
        Collections.replaceAll(getCompany().getCompanyFolderList(), getChosenEntity(), getChosenEntity());
    }
    
    @Transactional
    public void onNewCompany(@Observes(during = TransactionPhase.AFTER_SUCCESS) @CreatedEntityEvent Company c){
        if(c.getCompanyFolderList()==null){
            c.setCompanyFolderList(new ArrayList<CompanyFolder>());
        }
        setEntityList(c.getCompanyFolderList());
        CompanyFolder work=createFolderWorkWithUs(c);
        c.getCompanyFolderList().add(work);
        setCompany(c);
    }
    
    public CompanyFolder createFolderWorkWithUs(Company company){
        CompanyFolder workWithUs= new CompanyFolder();
        workWithUs.setCompanyFolderType(companyFolderTypeFacade.getWorkWithUsFolderType());
        workWithUs.setCreationDate(new Date());
        FacesContext context=FacesContext.getCurrentInstance();
        Locale locale=context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        String workWithUsMesage=rb.getString("WorkWithUs");
        workWithUs.setDescription(workWithUsMesage);
        workWithUs.setIdCompany(company);
        workWithUs.setCompanyAdquiredProfileFolderList(new ArrayList<CompanyAdquiredProfileFolder>());
        workWithUs.setSearchList(new ArrayList<Search>());
        precreateCompanyFolder.fire(workWithUs);
        companyFolderFacade.create(workWithUs);
        createdCompanyFolder.fire(workWithUs);
        return workWithUs;
    }

    /**
     * @return the newFolder
     */
    public CompanyFolder getNewFolder() {
        return newFolder;
    }

    /**
     * @param newFolder the newFolder to set
     */
    public void setNewFolder(CompanyFolder newFolder) {
        this.newFolder = newFolder;
    }
    
    public String viewAllProfiles(){
        viewAllProfilesEvent.fire(0);
        setChosenEntity(null);
        return "/views/company/folder/NewViewAllProfilesView?faces-redirect=true";
    }
    
    /**
     * @param folder
     * @param numberOfCandidates
     * @return the previewCandidateList
     */
    public List<CompanyAdquiredProfile> getPreviewCandidateList(CompanyFolder folder, Integer numberOfCandidates) {
        List<CompanyAdquiredProfile> validCandidates=new ArrayList<CompanyAdquiredProfile>();
        if(folder.getCompanyFolderType().getIdCompanyFolderType()==2){
            for (CompanyAdquiredProfile companyAdquiredProfile : folder.getCompanyAdquiredProfileList()) {
                if(companyAdquiredProfile.getCandidate().getBirthDay()!=null && companyAdquiredProfile.getCandidate().getBodyFormal()!=null && companyAdquiredProfile.getCandidate().getBustBlackWhite()!=null && companyAdquiredProfile.getCandidate().getBustColor()!=null && companyAdquiredProfile.getCandidate().getName()!=null && companyAdquiredProfile.getCandidate().getLastName()!=null && !validCandidates.contains(companyAdquiredProfile)){
                    validCandidates.add(companyAdquiredProfile);
                }
            }
        }
        else{
            for (CompanyAdquiredProfileFolder companyAdquiredProfileFolder : folder.getCompanyAdquiredProfileFolderList()) {
                validCandidates.add(companyAdquiredProfileFolder.getCompanyAdquiredProfile());
            }
        }
        if(validCandidates.size()>numberOfCandidates){
            validCandidates = new ArrayList<CompanyAdquiredProfile>(validCandidates.subList(validCandidates.size()-(numberOfCandidates+1), validCandidates.size()-1));
        }
        return validCandidates;
    }

    @Override
    public String redirectToViewEntity() {
        return "/views/company/folder/NewViewFolderView?faces-redirect=true";    
    }

}
